package work.xkong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import work.xkong.bo.TimeComparison;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Course;
import work.xkong.entity.Student;
import work.xkong.entity.StudentCourse;
import work.xkong.mapper.CourseMapper;
import work.xkong.mapper.StudentCourseMapper;
import work.xkong.mapper.StudentMapper;
import work.xkong.service.IStudentCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import work.xkong.vo.inputvo.StudentCourseInputVo;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author NuanYang
 * @since 2022-11-21
 */
@Service
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourse> implements IStudentCourseService {
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TimeComparison timeComparison;

    @Override
    public ResponseRest saveCourse(StudentCourseInputVo sci) {
        //判断学生是否存在
        Student student = studentMapper.selectOne(
                new LambdaQueryWrapper<Student>().
                        eq(Student::getStudentId, sci.getStudentId())
        );

        if (student == null) {
            return ResponseRest.creatFail("该学员不存在，非法请求");
        }

        Course course = courseMapper.selectOne(
                new LambdaQueryWrapper<Course>()
                        .eq(Course::getCourseId, sci.getCourseId())
        );

        if (course == null) {
            return ResponseRest.creatFail("选课失败，该课程不存在");
        }

        List<String> studentCourseTimeList = studentCourseMapper.getStudentCourseTime(sci.getStudentId());

        for (String sctl : studentCourseTimeList) {
            if (timeComparison.timeConflictVs(sctl,course.getCourseTime())) {
                return ResponseRest.creatFail("选课失败，该课程时间冲突");
        }
        }

        StudentCourse sc = new StudentCourse();
        sc.setScStudentId(sci.getStudentId());
        sc.setScCourseId(sci.getCourseId());

        boolean res = studentCourseMapper.insert(sc) > 0;

        if (!res) {
            return ResponseRest.creatFail("选课失败，请联系管理员");
        }

        if (course.getCourseSelectedCount().equals(course.getCourseMaxSize())) {
            return ResponseRest.creatFail("班级人数已满，请刷新页面重新选择");
        }

        course.setCourseSelectedCount(course.getCourseSelectedCount() + 1);

        boolean result = courseMapper.updateById(course) > 0;

        if (!result) {
            return ResponseRest.creatFail("网络异常波动，选课失败");
        }

        return ResponseRest.creatSuccess("恭喜你选课成功");
    }

    @Override
    public ResponseRest removeCourse(StudentCourseInputVo sci) {
        //判断学生是否存在
        Student student = studentMapper.selectOne(
                new LambdaQueryWrapper<Student>().
                        eq(Student::getStudentId, sci.getStudentId())
        );

        if (student == null) {
            return ResponseRest.creatFail("该学员不存在，非法请求");
        }

        Course course = courseMapper.selectOne(
                new LambdaQueryWrapper<Course>()
                        .eq(Course::getCourseId, sci.getCourseId())
        );

        if (course == null) {
            return ResponseRest.creatFail("退课失败，该课程不存在");
        }

        boolean res = studentCourseMapper.delete(
                new LambdaQueryWrapper<StudentCourse>()
                    .eq(StudentCourse::getScCourseId,sci.getCourseId())
                    .eq(StudentCourse::getScStudentId,sci.getStudentId())
        ) > 0;

        if (!res) {
            return ResponseRest.creatFail("退课失败，请联系管理员");
        }

        course.setCourseSelectedCount(course.getCourseSelectedCount() - 1);

        boolean result = courseMapper.updateById(course) > 0;

        if (!result) {
            return ResponseRest.creatFail("网络异常波动，退课失败");
        }

        return ResponseRest.creatSuccess("恭喜你退课成功");
    }
}
